package com.sumainfor.pc.market.service;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.ManagerMapper;
import com.sumainfor.pc.market.feign.EpcFeign.EpcFeign;
import com.sumainfor.pc.market.feign.GmioFeign.GmioFeign;
import com.sumainfor.pc.market.feign.ZuulFeign.ZuulFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 总经办相关
 * @Auther: zhlu
 * @Date: 2019/5/11 16:20
 * @Description:
 */
@Service
public class ManagerService {

    Logger log= LoggerFactory.getLogger(ManagerService.class);

    @Autowired
    ManagerMapper ManagerMapper;

    @Autowired
    ZuulFeign ZuulFeign;

    @Autowired
    GmioFeign GmioFeign;

    @Autowired
    EpcFeign EpcFeign;

    /**
     * 功能描述: 
     * 获取总经办运营部相关统计
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/11 16:18
     */
    public List<Map<String,Object>> getManagerGmioCount(Map<String,Object>params){

        //获取总经办运营部相关统计-新签房源
        List<Map<String,Object>>getManagerGmioCount0=ManagerMapper.getManagerGmioCount0(params);

        //获取总经办运营部相关统计-续签房源
        List<Map<String,Object>>getManagerGmioCount1=ManagerMapper.getManagerGmioCount1(params);

        //获取总经办运营部相关统计-本月新收
        List<Map<String,Object>>getManagerGmioCount3=ManagerMapper.getManagerGmioCount3(params);

        //获取总经办运营部相关统计-黑名单
        List<Map<String,Object>>getManagerGmioCount9=ManagerMapper.getManagerGmioCount9(params);
        log.info("-----------------------");
        log.info("--------params:"+params);
        log.info("-----------------------");
        //所有服务中心
        List<Map<String,Object>>jsonResult=ZuulFeign.getPostDeptList(params);
        //初始化。先给所有服务中心给默认值
        for(Map<String,Object>map:jsonResult){
            map.put("gmioCount0",0);
            map.put("gmioCount1",0);
            map.put("gmioCount3",0);
            map.put("gmioCount9",0);
        }
        if(getManagerGmioCount0.size()>0)jsonResult= ListMapMergeUtil.ListMergeUtils(jsonResult,getManagerGmioCount0,"DeptId");
        if(getManagerGmioCount1.size()>0)jsonResult= ListMapMergeUtil.ListMergeUtils(jsonResult,getManagerGmioCount1,"DeptId");
        if(getManagerGmioCount3.size()>0)jsonResult= ListMapMergeUtil.ListMergeUtils(jsonResult,getManagerGmioCount3,"DeptId");
        if(getManagerGmioCount9.size()>0)jsonResult= ListMapMergeUtil.ListMergeUtils(jsonResult,getManagerGmioCount9,"DeptId");

        //运营
        JsonResult jsonResult1=GmioFeign.getManagerGmioCount(params);
        List<Map<String,Object>>gmioList=(List<Map<String,Object>>) jsonResult1.getResult();
        //工程
        JsonResult jsonResult2=EpcFeign.getManagerGmioCount(params);
        List<Map<String,Object>>epcList=(List<Map<String,Object>>) jsonResult2.getResult();
        jsonResult= ListMapMergeUtil.ListMergeUtils(jsonResult,gmioList,"DeptId");
        jsonResult= ListMapMergeUtil.ListMergeUtils(jsonResult,epcList,"DeptId");
        return jsonResult;
    }

    /**
     * 功能描述: 
     * 获取总经办列表详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/15 15:11
     */
    public JsonResult getManagerGmioCountList(Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        String Option=params.get("Option").toString();
        pager.setPageno((Integer)params.get("pageno"));
        pager.setPagesize((Integer)params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();

        List<Map<String,Object>>dateList=new ArrayList<Map<String,Object>>();
        Integer count=0;

        //获取所有的后台用户资料
        JsonResult jsonResult=ZuulFeign.getAllUserIdAndName();
        List<Map<String,Object>>getAllUserIdAndName=(List<Map<String,Object>>)jsonResult.getResult();

        switch (Option){
            case "0":
                count=ManagerMapper.getManagerGmioCount0Integer(params);
                if(count==0){
                    result=pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,count);
                }else{
                    dateList=ManagerMapper.getManagerGmioCount0List(params);
                    dateList=ListMapMergeUtil.ListMergeUtils(dateList,getAllUserIdAndName,"userId");
                    result=pageUtils.getJsonResult(dateList, params, count);
                }
                break;
            case "1":
                count=ManagerMapper.getManagerGmioCount1Integer(params);
                if(count==0){
                    result=pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,count);
                }else{
                    dateList=ManagerMapper.getManagerGmioCount1List(params);
                    result=pageUtils.getJsonResult(dateList, params, count);
                }
                break;
            case "2":
                result=GmioFeign.getManagerGmioCountList(params);
                break;
            case "3":
                count=ManagerMapper.getManagerGmioCount3Integer(params);
                if(count==0){
                    result=pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,count);
                }else{
                    dateList=ManagerMapper.getManagerGmioCount3List(params);
                    dateList=ListMapMergeUtil.ListMergeUtils(dateList,getAllUserIdAndName,"userId");
                    result=pageUtils.getJsonResult(dateList, params, count);
                }
                break;
            case "4":
                result=GmioFeign.getManagerGmioCountList(params);
                break;
            case "5":
                result=EpcFeign.getManagerGmioCountList(params);
                break;
            case "6":
                result=GmioFeign.getManagerGmioCountList(params);
                break;
            case "7":
                result=GmioFeign.getManagerGmioCountList(params);
                break;
            case "8":
                result=GmioFeign.getManagerGmioCountList(params);
                break;
            case "9":
                count=ManagerMapper.getManagerGmioCount9Integer(params);
                if(count==0){
                    result=pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,count);
                }else{
                    dateList=ManagerMapper.getManagerGmioCount9List(params);
                    dateList=ListMapMergeUtil.ListMergeUtils(dateList,getAllUserIdAndName,"userId");
                    result=pageUtils.getJsonResult(dateList, params, count);
                }
                break;
        }
        return result;
    }

}
